package com.zz.ui_demo_wedgit;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.util.Log;

import com.zz.ui_demo_wedgit.sqlite.FeedReaderDBHelper;
import com.zz.ui_demo_wedgit.sqlite.FeedReaderContract.FeedEntry;

import java.util.ArrayList;
import java.util.List;

public class SQLiteDemoActivity extends AppCompatActivity {
    public static final String TAG = "MyDebug";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sqlite_demo);
        dbTest();
    }

    private void dbTest() {
//        Log.i(TAG, "输入数据返回的ID:" + insert("title", "subtitle"));
        Log.i(TAG, "查询数据集的大小：" + query().size());
         Log.i(TAG, "数据更新影响的记录数" + update());
        // Log.i(TAG, "数据删除影响的记录数" + delete());
    }

    private List<Long> query() {
        FeedReaderDBHelper dbHelper = new FeedReaderDBHelper(getApplicationContext());
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String[] projection = {
                BaseColumns._ID, FeedEntry.COLUMN_NAME_TITLE, FeedEntry.COLUMN_NAME_SUBTITLE
        };
        String selection = FeedEntry.COLUMN_NAME_TITLE + "=?";
        String[] selectionArgs = new String[]{"title"};
        String sortOrder = FeedEntry.COLUMN_NAME_SUBTITLE + " DESC";
        Cursor cursor = db.query(FeedEntry.TABLE_NAME, projection, selection,
                selectionArgs, null, null, sortOrder);
        List<Long> itemIds = new ArrayList<>();
        while (cursor.moveToNext()) {
            long itemId = cursor.getLong(cursor.getColumnIndex(FeedEntry._ID));
            itemIds.add(itemId);
        }
        cursor.close();

        return itemIds;
    }

    private long insert(String title, String subtitle) {
        FeedReaderDBHelper dbHelper = new FeedReaderDBHelper(getApplicationContext());
        SQLiteDatabase db = dbHelper.getWritableDatabase();

        ContentValues contentValues = new ContentValues();
        contentValues.put(FeedEntry.COLUMN_NAME_TITLE, title);
        contentValues.put(FeedEntry.COLUMN_NAME_SUBTITLE, subtitle);

        long newRowId = db.insert(FeedEntry.TABLE_NAME, null, contentValues);
        return newRowId;
    }

    private int update() {
        FeedReaderDBHelper dbHelper = new FeedReaderDBHelper(getApplicationContext());
        SQLiteDatabase db = dbHelper.getWritableDatabase();

        String title = "newTitle";
        ContentValues contentValues = new ContentValues();
        contentValues.put(FeedEntry.COLUMN_NAME_TITLE, title);

        String selection = FeedEntry._ID + "=?";
        String[] selectionArgs = new String[]{"1"};
        int count = db.update(FeedEntry.TABLE_NAME, contentValues, selection, selectionArgs);

        return count;
    }

    private int delete() {
        FeedReaderDBHelper dbHelper = new FeedReaderDBHelper(getApplicationContext());
        SQLiteDatabase db = dbHelper.getWritableDatabase();

        String selection = FeedEntry.COLUMN_NAME_TITLE + " LIKE ?";
        String[] selectionArgs = new String[]{"%title%"};
        int deleteRow = db.delete(FeedEntry.TABLE_NAME, selection, selectionArgs);

        return deleteRow;
    }
}
